import 'package:flutter/material.dart';
import 'package:sqflite/sqflite.dart';
import 'package:ymt/db/user_database_provider.dart';
import 'package:path/path.dart';
import 'package:ymt/model/product.dart';
import 'package:ymt/utils/image.dart';

class FavoritePage extends StatelessWidget{
  @override
  Widget build(BuildContext context) {
    MyHome home = MyHome(context);
    Center center = Center(child: click(Text("清空"), () =>{
      home.state.clearAll()
    }),);
    Container rightAction = Container(child: center,margin: EdgeInsets.only(right: 20),);
    return MaterialApp(theme: new ThemeData.dark(), home: Scaffold(
      appBar: AppBar(title: _buildBackText(context),
        backgroundColor: ThemeData
            .dark()
            .scaffoldBackgroundColor,
        elevation: 0,
        actions: <Widget>[rightAction],), body: home,),);
  }

  _buildBackText(context) {
    return click(Row(children: <Widget>[
      Icon(Icons.arrow_back_ios),
      Expanded(child: Text("当前收藏", style: TextStyle(fontSize: 15)),)
    ],), ()=>Navigator.pop(context));
  }

  Widget click(child, onTap) {
    return GestureDetector(child: child, onTap: onTap,);
  }
}

class MyHome extends StatefulWidget{

  MyHomeState state;

  final _pageContext;

  MyHome(this._pageContext);

  @override
  State<StatefulWidget> createState() {
    state = MyHomeState(_pageContext);
    return state;
  }
}

class MyHomeState extends State<MyHome>{

  var _products = <Product>[];

  UserDatabaseProvider _userDatabaseProvider;

  final _pageContext;

  MyHomeState(this._pageContext);

  @override
  void initState() {
    loadAllFavorite();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return _buildGrid();
  }

  Widget _buildGrid() {
    return new GridView.builder(
      padding: EdgeInsets.only(left: 22, right: 22,),
      gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
          mainAxisSpacing: 12,
          crossAxisSpacing: 12,
          crossAxisCount: 4,
          childAspectRatio: 16 / 9),
      itemBuilder: (context, position) {
        if (_products != null && _products.isNotEmpty) {
          if (position < _products.length) {
            return _buildImage(_products[position].main_picture, position);
          }
        }
        return Container();
      }, itemCount: _products.length,
      physics: BouncingScrollPhysics(),
    );
  }

  Widget _buildImage(img, position){
    return CacheImage.imageClickToDetails(_pageContext, img, _products, position);
  }

  loadAllFavorite() async {
    var databasesPath = await getDatabasesPath();
    String path = join(databasesPath, 'user.db');
    _userDatabaseProvider = UserDatabaseProvider();
    await _userDatabaseProvider.open(path);
    List<Product> allFavorite = await _userDatabaseProvider.allProduct();
    setState(() {
      _products = allFavorite;
    });
  }

  clearAll() async {
    await _userDatabaseProvider.deleteAll();
    setState(() {
      _products.clear();
      //_products = <Product>[];
    });
  }
}
